package cn.com.gridinfo.sooc.core.home.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.mobads.SplashAd;
import com.baidu.mobads.SplashAdListener;
import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.Log;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.common.Const;
import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.core.login.activity.LoginActivity;
import cn.com.gridinfo.sooc.utils.IntentUtil;


/**
 * 开屏广告页面
 *
 * @author jieli
 */
public class AdsActivity extends BaseSoocActivity {

    @InjectView(R.id.ads_splashview)
    RelativeLayout adsSplashview;//展示百度广告
    @InjectView(R.id.pass_timer)
    TextView passTimer;//跳过倒计时

    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);//全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        View view = View.inflate(this, R.layout.activity_ads, null);
        setContentView(view);
        ButterKnife.inject(this);
        //百度开屏匡高
        baiduKaiPingAds();

        timer.start();
    }

    /**
     * 跳过倒计时的点击
     */
    @OnClick(R.id.pass_timer)
    void onPassTimerClick(){
        goHome();
        timer.cancel();
    }

    private CountDownTimer timer = new CountDownTimer(6000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            passTimer.setText("跳过 " + (millisUntilFinished / 1000));
        }

        @Override
        public void onFinish() {
            //关闭页面，进入主页
            passTimer.setClickable(false);
            goHome();
        }
    };

    /**
     * 百度开屏个广告
     */
    private void baiduKaiPingAds() {

//        百度开屏广告
        adsSplashview = ((RelativeLayout) findViewById(R.id.ads_splashview));

        SplashAdListener listener = new SplashAdListener() {
            @Override
            public void onAdDismissed() {
                Log.d("RSplashActivity--onAdDismissed");
//                jumpWhenCanClick();// 跳转至您的应用主界面
            }

            @Override
            public void onAdFailed(String arg0) {
                Log.d("RSplashActivity--onAdFailed");
//                jump();
            }

            @Override
            public void onAdPresent() {
                Log.d("RSplashActivity--onAdPresent");
            }

            @Override
            public void onAdClick() {
                Log.d("RSplashActivity--onAdClick");
                //设置开屏可接受点击时，该回调可用
            }
        };

        /**
         * 构造函数：百度开屏广告
         * new SplashAd(Context context, ViewGroup adsParent,
         * 				SplashAdListener listener,String adPlaceId, boolean canClick, SplashType splashType);
         */
        new SplashAd(this, adsSplashview, listener, Const.baidu_splashAd_id, true, SplashAd.SplashType.REAL_TIME);

    }

    /**
     * 当设置开屏可点击时，需要等待跳转页面关闭后，再切换至您的主窗口。故此时需要增加waitingOnRestart判断。
     * 另外，点击开屏还需要在onRestart中调用jumpWhenCanClick接口。
     */
    public boolean waitingOnRestart = false;

    private void jumpWhenCanClick() {
        Log.d("test--this.hasWindowFocus():" + this.hasWindowFocus());
        if (this.hasWindowFocus() || waitingOnRestart) {
            //自动登录
            goHome();
        } else {
            waitingOnRestart = true;
        }

    }

    /**
     * 不可点击的开屏，使用该jump方法，而不是用jumpWhenCanClick
     */
    private void jump() {
        //自动登录
        goHome();
    }


    /**
     * 自动登录
     */
    private void goHome() {

        String toPage = getIntent().getStringExtra("toPage");

        //自动登录
        if (toPage.equals("toLogin"))

        {
            IntentUtil.intentSlidIn(this, LoginActivity.class, 00);
            noAnimfinish();

        } else

        {
            IntentUtil.intentSlidIn(this, HomeActivity.class, 00);
            noAnimfinish();
        }
    }


    //按返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
